<template>
  <div class="form-info-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="起止时间:">
        <el-form-item>
          <el-date-picker type="date" placeholder="开始时间" style="width: 100%"
          v-model="queryParams.startDate"
          value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <span class="text-gray-500 mar-r-13">至</span>
        <el-form-item>
          <el-date-picker type="date" placeholder="结束时间" style="width: 100%"
          v-model="queryParams.endDate"
          value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item>
          <div class="flex">
            <div v-for="(item, index) of btnList" :key="index" class="btn-box cursor-p pad-l-14 font-14 pad-r-14 mar-l-20"
            :class="index === btnIndex ? 'active-btn-box': 'disactive-btn-box'" @click="changeBtn(index)">{{ item }}</div>
          </div>
        </el-form-item>
      </el-form-item>
    </el-form>
    <el-form :inline="true" class="demo-form-inline flex">
      <el-form-item label="订单搜索:">
        <el-input placeholder="输入订单号" v-model="queryParams.orderNo"/>
      </el-form-item>
      <el-form-item label="售后状态:" class="space">
        <el-select v-model="queryParams.afterSaleFlag" placeholder="请选择">
          <el-option label="有售后" value="1" />
          <el-option label="无售后" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <div class="search-btn cursor-p font-14 flex flex-center" @click="handleSearch">查询</div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { formatTimeToDate } from '@/api/common/date'

export default defineComponent({
  setup(props,content) {
    const btnList:string[] = ['今', '昨', '7天', '1个月', '3个月']
    const btnIndex = ref(-1)
    const queryParams = ref({
      afterSaleFlag: '',
      orderNo: '',
      startDate: '',
      endDate: ''
    })

    const handleSearch = () => {
      if (queryParams.value.startDate) {
        queryParams.value.startDate = queryParams.value.startDate + " 00:00:00"
      }
      if (queryParams.value.endDate) {
        queryParams.value.endDate = queryParams.value.endDate + " 23:59:59"
      }
      content.emit("handleSearch",queryParams)
    }

    const changeBtn = (index:number):void => {
      btnIndex.value = index
      switch (index) {
        case 0:
          queryParams.value.startDate = formatTimeToDate(new Date())
          queryParams.value.endDate = formatTimeToDate(new Date())
          break
        case 1:
          const date1 = new Date()
          date1.setTime(date1.getTime() - 3600 * 1000 * 24)
          queryParams.value.startDate = formatTimeToDate(date1)
          queryParams.value.endDate = formatTimeToDate(date1)
          break
        case 2:
          const date2 = new Date()
          date2.setTime(date2.getTime() - 3600 * 1000 * 24 * 7)
          queryParams.value.startDate = formatTimeToDate(date2)
          queryParams.value.endDate = formatTimeToDate(new Date())
          break
        case 3:
          const date3 = new Date()
          date3.setTime(date3.getTime() - 3600 * 1000 * 24 * 30)
          queryParams.value.startDate = formatTimeToDate(date3)
          queryParams.value.endDate = formatTimeToDate(new Date())
          break
        case 4:
          const date4 = new Date()
          date4.setTime(date4.getTime() - 3600 * 1000 * 24 * 30 * 3)
          queryParams.value.startDate = formatTimeToDate(date4)
          queryParams.value.endDate = formatTimeToDate(new Date())
          break
        default:
      }
      if (queryParams.value.startDate) {
        queryParams.value.startDate = queryParams.value.startDate + " 00:00:00"
      }
      if (queryParams.value.endDate) {
        queryParams.value.endDate = queryParams.value.endDate + " 23:59:59"
      }
      content.emit("handleSearch",queryParams)
    }
    return {
      btnList,
      btnIndex,
      changeBtn,
      queryParams,
      handleSearch
    }
  }
})
</script>

<style lang="scss" scoped>
.form-info-container {
  padding-bottom: 24px;
  border-bottom: 1px solid #F5F6FA;
  .space {
    margin-left: 59px;
  }
  .btn-box {
    border: 1px solid #DCDFE6;
    border-radius: 4px;
  }
  .active-btn-box {
    color: #2DE2DD;
    background: #0C213F;
  }
  .disactive-btn-box {
    color: #323233;
    background: #fff;
  }
  .search-btn {
    width: 68px;
    height: 40px;
    background: #0C213F;
    border-radius: 4px;
    color: #fff;
  }
}
</style>
